#include "findpsd.h"
#include "ui_findpsd.h"
#include "login.h"

FindPsd::FindPsd(QWidget *parent) :
	QDialog(parent),
	ui(new Ui::FindPsd) {
	ui->setupUi(this);
	socket = new QTcpSocket(this);
	connect(socket, SIGNAL(readyRead()), this, SLOT(readMsg()));
	connect(socket, SIGNAL(error(QAbstractSocket::SocketError)),
	        this, SLOT(displayError(QAbstractSocket::SocketError)));  //发生错误时执行displayError函数
	ui->lineEdit_Name->setFocus();  //鼠标定位
}

FindPsd::~FindPsd() {
	delete this->socket;
	delete ui;
}


void FindPsd::readMsg() {//读取传输过来的消息
	QString data = socket->readAll();
	QStringList list = data.split("#");
	if (list[0] == "c" && list[1] == "true") {
		QString str = list[2];
		qDebug() << list;
		QMessageBox::information(this, "找回密码", str, QMessageBox::Ok);
		FindPsd::close();
	} else if (list[0] == "c" && list[1] == "false" && list[2] == "0") {
		QMessageBox::information(this, "信息提示", "手机号错误,请重新输入!", QMessageBox::Ok);
		ui->lineEdit_Mail->clear();  //清空错误栏
		ui->lineEdit_Mail->setFocus();  //将鼠标重新定位
	} else if (list[0] == "c" && list[1] == "false" && list[2] == "4") {
		QMessageBox::information(this, "信息提示", "用户不存在，请重新输入!", QMessageBox::Ok);
		ui->lineEdit_Name->clear();
		ui->lineEdit_Mail->clear();;
		ui->lineEdit_Name->setFocus();
	}

}

void FindPsd::on_pushButton__clicked() {
	socket->abort();   //取消已有的连接
	socket->connectToHost(IP, port); //连接服务器
	//等待连接成功
	if (!socket->waitForConnected(30000)) {
		qDebug() << "连接失败!";
		return;
	}
	qDebug() << "连接成功!";
	QString string1 = ui->lineEdit_Name->text();//用户名
	QString string2 = ui->lineEdit_Mail->text();//手机号
	QString cs = "c";
	QString data = cs + "#" + string1 + "#" + string2;
	socket->write(data.toUtf8());
}
